﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace Shiny.SmartTravel.Data.Local
{
    /// <summary>
    /// A Province entity for local use
    /// </summary>
    public class Province : INotifyPropertyChanged
    {
        /// <summary>
        /// Empty ctor for province
        /// </summary>
        public Province()
        {
        }

        /// <summary>
        /// Gets or set the unique id of a Province object.
        /// </summary>
        public int Id { get; set; }

        private string name;
        /// <summary>
        /// Gets or sets the name of a Province object.
        /// </summary>
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        private string intro;
        /// <summary>
        /// Gets or sets a string value indicates the intro of a Province object.
        /// </summary>
        public string Intro
        {
            get
            { return intro; }
            set
            {
                intro = value;
                OnPropertyChanged("Intro");
            }
        }

        /// <summary>
        /// Gets or sets a string value indicates where the image located at.
        /// </summary>
        public string Image { get; set; }

        /// <summary>
        /// Gets or sets a string value indicates th domain used by a Province object.
        /// </summary>
        public string Domain { get; set; }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Notify when property changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        /// <summary>
        /// notify when property has changed
        /// </summary>
        /// <param name="name"></param>
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }

        /// <summary>
        /// Gets the Name of a Province
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Name;
        }
    }

    /// <summary>
    /// The collection class of Province object
    /// </summary>
    public class ProvincesCollection : ObservableCollection<Province>
    {
        private string dataFile = "Provinces.xml";

        /// <summary>
        /// Empty ctor.
        /// </summary>
        public ProvincesCollection()
        {
            try
            {
                IEnumerable<Province> provinces = LoadAllProvinces();
                foreach (var p in provinces)
                {
                    Add(p);
                }
            }
            catch (Exception ex)
            {
                throw new LocalDataException("LocalData Error! Please DO NOT modify the files under LocalData folder, great thanks.", ex);
            }
        }

        /// <summary>
        /// Load all provinces
        /// </summary>
        /// <returns></returns>
        private IEnumerable<Province> LoadAllProvinces()
        {
            string path = string.Format("{0}\\{1}", Settings.LocalDataFolder, dataFile);
            XDocument xdoc = XDocument.Load(path);
            IEnumerable<Province> provinces = from p in xdoc.Element("XutourProvinces").Elements("Province")
                                              select new Province { Id = (int)p.Element("Id"), Name = (string)p.Element("Name"), Image = (string)p.Element("Image"), Intro = (string)p.Element("Intro"), Domain = (string)p.Element("Domain") };
            return provinces;
        }
    }
}
